//
//  WBN_FM_MainViewController.m
//  GoodManNews
//
//  Created by 王博男 on 16/1/11.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "WBN_FM_MainViewController.h"
#import "Masonry.h"
#import "WBN_FM_MainTableViewCell.h"
#import "GoodManNews_AFNetTool.h"
#import "UIImageView+WebCache.h"
#import "WBN_FM_MainModel.h"
#import "WBN_FM_CardDetailsModel.h"
#import "WBN_FM_PlayViewController.h"
#import "WBN_FM_PlayMoreViewController.h"
#import "WBN_FM_MyCacheViewController.h"
#import "WBN_TV_MainViewController.h"
#import "GoodManCollectionView.h"

#define FMURL @"http://api.3g.ifeng.com/api_fm_homepage?gv=4.6.9&av=0&proid=ifengnews&os=ios_9.2&vt=5&screen=750x1334&publishid=4002&uid=bf668f34bb83eff6208cbc233768f3ea93644221"

@interface WBN_FM_MainViewController () <UITableViewDataSource, UITableViewDelegate, WBN_FM_MainTableViewCellDelegate>

@property (nonatomic, retain) UITableView *tableV;

@property (nonatomic, retain) NSMutableArray *arr;

@property (nonatomic, retain) UIView *playingV;
@property (nonatomic, retain) WBN_FM_PlayViewController *playerVC;

@property (nonatomic, retain) NSMutableArray *lunboArr;

@end

@implementation WBN_FM_MainViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.arr = [NSMutableArray array];
    self.lunboArr = [NSMutableArray array];
    
    self.playerVC = [WBN_FM_PlayViewController sharePlayerViewController];
    
    [self createTableView];
    [self getData];
}


- (void)createTableView
{
    self.tableV = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    [_tableV release];
    
    self.tableV.rowHeight = 290;
    
    
    [self.tableV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view);
        make.bottom.equalTo(self.view);
        make.width.equalTo(self.view);
        
    }];
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor colorWithWhite:0.798 alpha:1.000];
    self.tableV.tableHeaderView = view;
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.tableV);
        make.width.equalTo(self.tableV);
        make.height.equalTo(@50);
        
    }];
    
    
    UIView *cacheView = [UIView new];
    [view addSubview:cacheView];
    
    [cacheView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(view.mas_bottom).with.offset(-50);
        make.bottom.equalTo(view.mas_bottom);
        make.left.equalTo(view);
        make.width.equalTo(@120);
        
    }];
    
    
    UIImageView *imageV = [UIImageView new];
    imageV.image = [UIImage imageNamed:@"list_FM_myCache_icon_night@3x"];
    [cacheView addSubview:imageV];
    
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(cacheView).with.offset(10);
        make.left.equalTo(cacheView).with.offset(10);
        make.bottom.equalTo(cacheView).with.offset(-10);
        make.width.equalTo(@40);
        
    }];
    
    
    UILabel *label = [UILabel new];
    label.text = @"我的缓存";
    label.textColor = [UIColor whiteColor];
    [cacheView addSubview:label];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(imageV.mas_top);
        make.left.equalTo(imageV.mas_right).with.offset(10);
        make.bottom.equalTo(imageV.mas_bottom);
        make.width.equalTo(@70);
        
    }];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [cacheView addSubview:button];
    [button addTarget:self action:@selector(myCacheButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.equalTo(cacheView);
        
    }];
    
    
    
    // TV
    UIButton *TVButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [view addSubview:TVButton];
    [TVButton setTitle:@"TV" forState:UIControlStateNormal];
    [TVButton addTarget:self action:@selector(TVButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    [TVButton mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(cacheView);
        make.left.equalTo(cacheView.mas_right);
        make.bottom.equalTo(cacheView);
        make.width.equalTo(cacheView);
        
    }];
    
    
    
    UIButton *playB = [UIButton buttonWithType:UIButtonTypeCustom];
    [view addSubview:playB];
    [playB setTitle:@"正在播放" forState:UIControlStateNormal];
    [playB addTarget:self action:@selector(playButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    [playB mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.bottom.equalTo(cacheView);
        make.left.equalTo(TVButton.mas_right);
        make.right.equalTo(view);
        
    }];
    
    
    return view;
}

// TV
- (void)TVButtonAction
{
    WBN_TV_MainViewController *tvVC = [WBN_TV_MainViewController new];
    [self.navigationController pushViewController:tvVC animated:YES];
}

// 正在播放
- (void)playButtonAction
{
    [self.navigationController pushViewController:self.playerVC animated:YES];
}


// 点击进入我的缓存
- (void)myCacheButtonAction
{
    WBN_FM_MyCacheViewController *myCacheVC = [WBN_FM_MyCacheViewController new];
    [self.navigationController pushViewController:myCacheVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"cell";
    WBN_FM_MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (!cell) {
        cell = [WBN_FM_MainTableViewCell new];
    }
    
    cell.arr = [[self.arr objectAtIndex:indexPath.row] cardDetails];
    
    cell.label.text = [[self.arr objectAtIndex:indexPath.row] cardTitle];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.count = indexPath.row;
    
    cell.delegate = self;
    
    return cell;
}

// 进入详情
- (void)getInDetailWithNum1:(NSInteger)num1 Nmu2:(NSInteger)num2
{
    WBN_FM_MainModel *model = [self.arr objectAtIndex:num1];
    NSMutableArray *arr = model.cardDetails;
    WBN_FM_CardDetailsModel *cdModel = [arr objectAtIndex:num2];
    
    
    self.playerVC.rid = cdModel.mId;

    [self.navigationController pushViewController:self.playerVC animated:YES];
}

// 进入More
- (void)getInMoreDetailWithNum:(NSInteger)num
{
    WBN_FM_MainModel *model = [self.arr objectAtIndex:num];
    
    WBN_FM_PlayMoreViewController *moreVC = [WBN_FM_PlayMoreViewController new];
    
    moreVC.nodeId = model.nodeId;
    moreVC.str = model.cardTitle;
    
    [self.navigationController pushViewController:moreVC animated:YES];
}


- (void)getData
{
    [GoodManNews_AFNetTool getNetWithURL:FMURL body:nil headFile:nil responseStyle:WBNJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableDictionary *dic = [responseObject objectForKey:@"data"];
        NSMutableArray *arr = [dic objectForKey:@"cardList"];
        self.arr = [WBN_FM_MainModel baseModelWithArr:arr];
        
        for (WBN_FM_MainModel *model in self.arr) {
            
            model.cardDetails = [WBN_FM_CardDetailsModel baseModelWithArr:model.cardDetails];

        }
        
        
        // 轮播图片
        NSMutableArray *picArr = [dic objectForKey:@"focus"];
        for (NSDictionary *tempDic in picArr) {
            [self.lunboArr addObject:[tempDic objectForKey:@"image"]];
        }
        
        
        [self.tableV reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
